package com.ddwl.user.controller;

import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.bo.user.MenuAddBo;
import com.ddwl.schema.bo.user.MenuEditBo;
import com.ddwl.schema.vo.user.MenuVo;
import com.ddwl.user.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


/**
 * 菜单接口
 */
@Api(tags = "菜单服务")
@RequestMapping("/menu")
@Auth(product = BaseEnum.ProductEnum.CONSOLE)
@RestController
public class MenuController extends BaseController {

    @Resource
    private MenuService menuService;

    @PostMapping("/create")
    @ApiOperation("创建菜单")
    public RestVo create(@Valid @RequestBody MenuAddBo bo) {
        fillBaseField(bo);
        menuService.create(bo);
        return RestVo.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改菜单")
    public RestVo update(@Valid @RequestBody MenuEditBo bo) {
        fillBaseField(bo);
        menuService.update(bo);
        return RestVo.ok();
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除菜单")
    public RestVo delete(@RequestParam String id) {
        menuService.delete(id);
        return RestVo.ok();
    }

    @GetMapping("/detail")
    @ApiOperation("获取菜单明细")
    public RestVo<MenuVo> detail() {
        return RestVo.ok(menuService.getById(getUserId()));
    }


    @PostMapping("/tree")
    @ApiOperation("查询树结构的权限")
    public RestVo<List<MenuVo>> tree() {
        return RestVo.ok(menuService.tree());
    }

    @PostMapping("/changeId")
    @ApiOperation("把id反转成时间戳")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo changeId(){
        menuService.changeId();
        return RestVo.ok();
    }

}
